package com.sdkj.seckill.rocketmqTest.primaryMessage;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * @Description 发送同步消息
 * @Author Jiang
 * @Date 2022/01/18 18:26
 * @Version 1.0.0
 */
//这种可靠性同步地方式使用的比较广泛，比如：重要的消息通知、短信通知
public class SyncProducer {
    public static void main(String[] args) throws Exception {
        //实例化消息生产者Producer
        DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
        //设置NameServer的地址
        producer.setNamesrvAddr("47.94.172.125:9876");
        //启动Producer实例
        producer.start();
        for (int i = 0; i < 100; i++) {
            //创建消息，并指定Topic,Tag和消息体
            Message msg = new Message("TopicTest",
                    "TagA",
                    ("Hello RocketMQ "+i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            //发送一个消息到broker
            SendResult sendResult = producer.send(msg);
            //通过sendResult返回消息是否成功送达
            System.out.printf("%s%n",sendResult);
        }
        //如果不再发送消息，关闭Producer实例
        producer.shutdown();
    }
}
